package nccloud.api.impl.pu.m25;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nc.bs.scmpub.query.SCMBillQuery;
import nc.impl.pu.m25.action.InvoiceDeleteAction;
import nc.itf.scmpub.reference.uap.pf.PfServiceScmUtil;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pu.m25.entity.InvoiceHeaderVO;
import nc.vo.pu.m25.entity.InvoiceItemVO;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pu.pub.enumeration.POEnumBillStatus;
import nc.vo.pub.BusinessException;
import nccloud.api.impl.pu.m25.fill.InvoiceSaveFillValue;
import nccloud.api.impl.pu.m25.fill.InvoiceUpdateFillValue;
import nccloud.api.impl.pu.pub.check.CheckClearCalculateFields;
import nccloud.api.impl.pu.pub.check.CheckProhibitUpdateFields;
import nccloud.api.pu.m25.IAPIInvoiceMaitain;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;



public class APIInvoiceMaitainImpl
  implements IAPIInvoiceMaitain
{
  public InvoiceVO[] save(InvoiceVO[] vos) throws BusinessException {
    for (InvoiceVO vo : vos) {
      vo.getParentVO().setStatus(2);
    }
    
    InvoiceVO[] fillVOs = (new InvoiceSaveFillValue()).setDefaultValue(vos);
    return (InvoiceVO[])PfServiceScmUtil.processBatch("SAVEBASE", "25", fillVOs, null, null);
  }

  
  public InvoiceVO[] update(InvoiceVO vo) throws BusinessException {
    if (vo == null) {
      return null;
    }
    try {
      InvoiceHeaderVO newVO = vo.getParentVO();
      String pk_invoice = newVO.getPk_invoice();
      if (StringUtils.isEmpty(pk_invoice)) {
        throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0151"));
      }
      SCMBillQuery<InvoiceVO> queryTool = new SCMBillQuery<InvoiceVO>(InvoiceVO.class);
      
      InvoiceVO[] invoiceVOs = (InvoiceVO[])queryTool.queryVOByIDs(new String[] { pk_invoice });
      if (ArrayUtils.isEmpty(invoiceVOs)) {
        throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0152"));
      }
      List<String> headProFields = CheckProhibitUpdateFields.getHeadProhibitFields("25");
      List<String> bodyProFields = CheckProhibitUpdateFields.getBodyProhibitFields("25");
      InvoiceHeaderVO origVO = invoiceVOs[0].getParentVO();
      if (origVO.getFbillstatus() == null || 0 != ((Integer)POEnumBillStatus.FREE
        .value())
        .compareTo(origVO.getFbillstatus())) {
        throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0153") + origVO.getVbillcode() + NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0143"));
      }
      origVO.setStatus(1);
      for (String attr : newVO.getAttributeNames()) {
        if (newVO.getAttributeValue(attr) != null) {

          
          if (headProFields.contains(attr) && 
            !newVO.getAttributeValue(attr).equals(origVO.getAttributeValue(attr))) {
            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0144") + attr + NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0145"));
          }
          origVO.setAttributeValue(attr, newVO.getAttributeValue(attr));
        } 
      }  if (vo.getChildrenVO() != null) {
        Map<String, InvoiceItemVO> pkbs = new HashMap<String, InvoiceItemVO>();
        for (InvoiceItemVO origBvo : invoiceVOs[0].getChildrenVO()) {
          origBvo.setStatus(1);
          pkbs.put(origBvo.getPk_invoice_b(), origBvo);
        } 
        for (InvoiceItemVO newBvo : vo.getChildrenVO()) {
          InvoiceItemVO roigBvo = (InvoiceItemVO)pkbs.get(newBvo.getPk_invoice_b());
          if (newBvo.getPk_invoice_b() == null || roigBvo == null) {
            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0154"));
          }
          Set<String> bfields = new HashSet<String>();
          
          CheckClearCalculateFields.dealCalculateFields(roigBvo, newBvo);
          for (String battr : newBvo.getAttributeNames()) {
            if (newBvo.getAttributeValue(battr) != null) {

              
              if (bodyProFields.contains(battr) && 
                !newBvo.getAttributeValue(battr).equals(roigBvo.getAttributeValue(battr))) {
                throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0147") + battr + NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0145"));
              }
              roigBvo.setAttributeValue(battr, newBvo.getAttributeValue(battr));
              bfields.add(battr);
            } 
          } 
        } 
      } 
      
      (new InvoiceUpdateFillValue()).setDefaultValue(invoiceVOs);
      return (InvoiceVO[])PfServiceScmUtil.processBatch("SAVEBASE", "25", invoiceVOs, null, null);
    } catch (Exception e) {
      throw new BusinessException(e.getMessage());
    } 
  }

  
  public void delete(InvoiceVO[] vos) throws BusinessException {
    for (InvoiceVO vo : vos) {
      Integer status = vo.getParentVO().getFbillstatus();
      
      if (!POEnumBillStatus.FREE.toInteger().equals(status) && 
        !POEnumBillStatus.NOPASS.toInteger().equals(status)) {
        throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4004100_0", "04004100-0155"));
      }
    } 
    (new InvoiceDeleteAction()).delete(vos, null);
  }



public void delete(String[] arg0) throws BusinessException {
	// TODO Auto-generated method stub
	
}



public InvoiceVO[] save(List<Map<String, Object>> arg0) throws BusinessException {
	// TODO Auto-generated method stub
	return null;
}



public InvoiceVO[] update(List<Map<String, Object>> arg0) throws BusinessException {
	// TODO Auto-generated method stub
	return null;
}
}